package com.usefullc.user.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.system.vo.TreeVo;
import com.usefullc.user.domain.AuthRes;
import com.usefullc.user.param.MenuParam;
import com.usefullc.user.query.AuthResQuery;
import com.usefullc.user.service.AuthResService;
import com.usefullc.user.service.component.UserManager;
import com.usefullc.user.vo.RoleAuthResTreeVo;
import com.usefullc.user.vo.UserVo;
import com.usefullc.user.vo.permission.AuthResourceVo;
import com.usefullc.user.vo.permission.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: Sampson
 * @date: 2022-01-10 15:28
 */
@RestController
@RequestMapping(path = "/user/authRes/")
@Api( tags="授权资源管理")
public class AuthResController extends BaseController {

    @Autowired
    private AuthResService authResService;


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取当前用户的授权资源", response = WebResult.class)
    @GetMapping(path = "getAuthResByCurrentUser", produces = "application/json")
    public WebResult getAuthResByCurrentUser(AuthResQuery query) {
        UserVo user = UserManager.getLoginUser();
        AuthResourceVo authResource = authResService.getAuthResourceByUserId(user.getId());
        return WebResultBuilder.successResult(authResource);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取列表", response = WebResult.class)
    @GetMapping(path = "list", produces = "application/json")
    public WebResult list(AuthResQuery query) {
        Pager<MenuVo> pager = authResService.listByPage(query);
        return WebResultBuilder.successResult(pager);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取树列表", response = WebResult.class)
    @GetMapping(path = "listTree", produces = "application/json")
    public WebResult listTree(AuthResQuery query) {
        List<TreeVo> dataList = authResService.listTree(query);
        return WebResultBuilder.successResult(dataList);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取权限资源数根据角色ID", response = WebResult.class)
    @GetMapping(path = "getAuthResTreeVoByRoleId", produces = "application/json")
    public WebResult getAuthResTreeVoByRoleId(@RequestParam Long roleId){
        RoleAuthResTreeVo authResTreeVo = authResService.getAuthResTreeVoByRoleId(roleId);
        return WebResultBuilder.successResult(authResTreeVo);
    }


    @ApiOperation(value = "保存菜单", response = WebResult.class)
    @PostMapping(path = "save", produces = "application/json")
    public WebResult save(@UsefulValid(save = true) @RequestBody MenuParam menuParam) {
        AuthRes authRes = authResService.menuParamToAuthRes(menuParam);
        authResService.saveAuthRes(authRes);
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "复制菜单", response = WebResult.class)
    @PostMapping(path = "copy", produces = "application/json")
    public WebResult copy(@UsefulValid(save = true) @RequestBody IdsParam param) {
        authResService.copy(param.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "修改菜单", response = WebResult.class)
    @PostMapping(path = "update", produces = "application/json")
    public WebResult update(@UsefulValid(update = true) @RequestBody MenuParam menuParam) {
        AuthRes authRes = authResService.menuParamToAuthRes(menuParam);
        authResService.updateAuthRes(authRes);
        return WebResultBuilder.successResult();
    }


    @ApiOperation(value = "删除菜单", response = WebResult.class)
    @PostMapping(path = "del", produces = "application/json")
    public WebResult del(@UsefulValid(del = true) @RequestBody IdsParam idsParam) {
        authResService.deletePhysByIds(idsParam.getIds());
        return WebResultBuilder.successResult();
    }

}
